const { AppError } = require('../middlewares/errorHandler');
const { logger } = require('../utils/logger');
const notificationService = require('../services/notification.service');

class NotificationController {
  /**
   * 发送通知
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async send(ctx) {
    const { type, recipients, template, data } = ctx.request.body;

    try {
      const result = await notificationService.send(type, recipients, template, data);
      ctx.status = 201;
      ctx.body = {
        status: '成功',
        data: { result }
      };
    } catch (error) {
      throw new AppError(400, error.message);
    }
  }

  /**
   * 获取通知模板
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async getTemplates(ctx) {
    const templates = await notificationService.getTemplates();
    ctx.body = {
      status: '成功',
      data: { templates }
    };
  }

  /**
   * 获取通知模板
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async getTemplate(ctx) {
    const { templateId } = ctx.params;
    const template = await notificationService.getTemplate(templateId);

    if (!template) {
      throw new AppError(404, '未找到模板');
    }

    ctx.body = {
      status: '成功',
      data: { template }
    };
  }

  /**
   * 创建通知模板
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async createTemplate(ctx) {
    const { name, type, subject, content, variables } = ctx.request.body;

    try {
      const template = await notificationService.createTemplate({
        name,
        type,
        subject,
        content,
        variables
      });

      ctx.status = 201;
      ctx.body = {
        status: '成功',
        data: { template }
      };
    } catch (error) {
      throw new AppError(400, error.message);
    }
  }

  /**
   * 更新通知模板
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async updateTemplate(ctx) {
    const { templateId } = ctx.params;
    const { name, type, subject, content, variables } = ctx.request.body;

    try {
      const template = await notificationService.updateTemplate(templateId, {
        name,
        type,
        subject,
        content,
        variables
      });

      if (!template) {
        throw new AppError(404, '未找到模板');
      }

      ctx.body = {
        status: '成功',
        data: { template }
      };
    } catch (error) {
      if (error instanceof AppError) throw error;
      throw new AppError(400, error.message);
    }
  }

  /**
   * 删除通知模板
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async deleteTemplate(ctx) {
    const { templateId } = ctx.params;
    const success = await notificationService.deleteTemplate(templateId);

    if (!success) {
      throw new AppError(404, '未找到模板');
    }

    ctx.body = {
      status: '成功',
      message: '模板删除成功'
    };
  }

  /**
   * 获取通知历史
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async getHistory(ctx) {
    const { page = 1, limit = 10, type, status } = ctx.query;
    const history = await notificationService.getHistory({
      page: parseInt(page),
      limit: parseInt(limit),
      type,
      status
    });

    ctx.body = {
      status: '成功',
      data: { history }
    };
  }

  /**
   * 获取通知状态
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async getNotificationStatus(ctx) {
    const { notificationId } = ctx.params;
    const status = await notificationService.getStatus(notificationId);

    if (!status) {
      throw new AppError(404, '未找到通知');
    }

    ctx.body = {
      status: '成功',
      data: { status }
    };
  }

  /**
   * 重新发送通知
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async resend(ctx) {
    const { notificationId } = ctx.params;
    
    try {
      const result = await notificationService.resend(notificationId);
      ctx.body = {
        status: '成功',
        data: { result }
      };
    } catch (error) {
      throw new AppError(400, error.message);
    }
  }

  /**
   * 获取通知渠道状态
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async getChannelStatus(ctx) {
    const { channel } = ctx.params;
    const status = await notificationService.getChannelStatus(channel);

    ctx.body = {
      status: '成功',
      data: { status }
    };
  }
}

module.exports = new NotificationController(); 